package bom.example.common.sql;



import java.io.BufferedReader;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

public class DBSql {

    private boolean flag = false;
    private String url;
    private String driver;
    private String password;
    private String username;
    private Connection connection;


    public DBSql(Map<String, String> stringMap) {
        this.url = stringMap.get("url");
        this.driver = stringMap.get("driver");
        this.password = stringMap.get("password");
        this.username = stringMap.get("username");
        this.flag = this.url != null && this.driver != null && this.username != null && this.password != null;
        this.initConnection();
    }

    public DBSql(String url, String driver, String username, String password) {
        this.url = url;
        this.driver = driver;
        this.username = username;
        this.password = password;
        this.flag = url != null && driver != null && username != null && password != null;
        this.initConnection();
    }

    public DBSql() throws Exception {
        throw new Exception("禁止这样使用");
    }

    public Map<String, Object> selectMapObjs(String sql) {
        Map<String, Object> objectMap = null;
        ResultSet resultSet = this.sqlExecuteQuery(sql);
        if (resultSet == null) {
            return objectMap;
        } else {
            String[] fields = this.getFeildList(resultSet);
            try {
                System.out.println(sql);
                if (resultSet.next()) {
                    objectMap = new HashMap(1);
                    for(int i = 0; i < fields.length; ++i) {
                        String feildName = fields[i];
                        Object value = resultSet.getObject(feildName);
                        objectMap.put(feildName, value);
                    }
                    return objectMap;
                }
            } catch (Exception var11) {
                var11.printStackTrace();
            }finally {
                closeConnection();
            }
            return objectMap;
        }
    }

    public List<Map<String, Object>> selectMapObjList(String sql) {
        ResultSet resultSet = this.sqlExecuteQuery(sql);
        List<Map<String, Object>> mapList = null;
        if (resultSet != null && this.flag) {
            String[] fields = this.getFeildList(resultSet);
            try {
                mapList = new ArrayList(50);
                while(resultSet.next()) {
                    Map<String, Object> stringObjectMap = new HashMap();
                    for(int i = 0; i < fields.length; ++i) {
                        String feildName = fields[i];
                        Object value = resultSet.getObject(feildName);
                        stringObjectMap.put(feildName, value);
                    }
                    mapList.add(stringObjectMap);
                }
            } catch (Exception var12) {
                var12.printStackTrace();
            }finally {
                closeConnection();
            }
            return mapList;
        } else {
            return mapList;
        }
    }

    public List<String> selectStringList(String sql){
        ResultSet resultSet = this.sqlExecuteQuery(sql);
        List<String> mapList = null;
        if (resultSet != null && this.flag) {
            String[] fields = this.getFeildList(resultSet);
            try {
                mapList = new ArrayList(50);
                while(resultSet.next()) {
                    String str=null;
                    for(int i = 0; i < fields.length; ++i) {
                        String feildName = fields[i];
                        Object value = resultSet.getObject(feildName);
                        if(value!=null){
                            str=String.valueOf(value);
                        }
                    }
                    if(str!=null){
                        mapList.add(str);
                    }
                }
            } catch (Exception var12) {
                var12.printStackTrace();
            }finally {
                closeConnection();
            }
            return mapList;
        } else {
            return mapList;
        }
    }



    public Boolean sqlListExecute(List<String> strings){
        if (strings==null){
            return false;
        }
        List<String> stringList=new ArrayList<>(1000);
        for (int i = 0; i < strings.size(); i++) {
            stringList.add(strings.get(i));
            if(i%1000==0){
                String excelSql=String.join(";",strings);
                this.sqlExecute(excelSql);
                stringList.clear();
            }
        }
        if(stringList.size()!=0){
            String excelSql = stringList.stream().collect(Collectors.joining(";"));
            this.sqlExecute(excelSql);
        }
        return true;
    }

    public <T> List<T> selectMapObjList(String sql,Class<T> cls) {
        ResultSet resultSet = this.sqlExecuteQuery(sql);
        List<T> mapList = null;
        if (resultSet != null && this.flag) {
            String[] fields = this.getFeildList(resultSet);
            Field[] tFields=cls.getDeclaredFields();
            try {
                mapList = new ArrayList(50);
                while(resultSet.next()) {
                    T t=cls.newInstance();
                    for(int i = 0; i < fields.length; ++i) {
                        String feildName = fields[i];
                        Field field=getFieldByName(feildName,tFields);
                        field.setAccessible(true);
                        Object value = resultSet.getObject(feildName);
                        field.set(t,value);
                    }
                    mapList.add(t);
                }
            } catch (Exception var12) {
                var12.printStackTrace();
            }finally {
                closeConnection();
            }
            return mapList;
        } else {
            return mapList;
        }
    }

    public Field getFieldByName(String name,Field[] fields){
        for (int i = 0; i < fields.length; i++) {
            Field field=fields[i];
            if(!name.equals(field.getName())){
                continue;
            }
            return field;
        }
        return null;
    }

    public boolean updateSql(String sql){
        return sqlExecute(sql);
    }

    public boolean sqlExecute(String sql) {
        boolean flag = false;
        Statement stmt = null;
        try {
            stmt = this.connection.createStatement();
            flag = stmt.execute(sql);
            return flag;
        } catch (Exception var8) {
            var8.printStackTrace();
        }finally {
            closeConnection();
        }
        return flag;
    }

    private String[] getFeildList(ResultSet rs) {
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            String[] lie = new String[rsmd.getColumnCount()];
            for(int i = 1; i < rsmd.getColumnCount() + 1; ++i) {
                lie[i - 1] = rsmd.getColumnName(i);
            }
            return lie;
        } catch (Exception var5) {
            var5.printStackTrace();
            return new String[0];
        }
    }

    private ResultSet sqlExecuteQuery(String sql) {
        Statement stmt = null;
        ResultSet rs = null;

        try {
            if (this.connection == null) {
                return null;
            } else {
                stmt = this.connection.createStatement();
                rs = stmt.executeQuery(sql);
                return rs;
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            return rs;
        }
    }

    private void initConnection() {
        try {
            Class.forName(this.driver);
            this.connection = DriverManager.getConnection(this.url, this.username, this.password);
        } catch (ClassNotFoundException var2) {
            var2.printStackTrace();
        } catch (SQLException var3) {
            var3.printStackTrace();
        }

    }


    private Map<String, String> stringMap(Map<String, String> stringMap) {
        Iterator var2 = stringMap.entrySet().iterator();
        while(var2.hasNext()) {
            Map.Entry<String, String> map = (Map.Entry)var2.next();
            String key = (String)map.getKey();
            stringMap.put(key.toLowerCase(), map.getValue());
            stringMap.remove(key, map.getValue());
        }
        return stringMap;
    }

    private void closeConnection() {
        try {
            this.connection.close();
        } catch (SQLException var2) {
            var2.printStackTrace();
        }
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Connection getConnection() {
        return connection;
    }


}
